<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 * 
 * LBB Solutions Oy 版权所有 @ 2013
 */
class Controller extends CController
{
	private $_memory_usage = 0.0;
	
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	/**
	 * @var array the header is to set the mata header data.
	 */
	public $header = array();
	
	public $meta_facebook = '';
	
	/**
	 * Init all controllers
	 */
	public function init() {
		parent::init();
		
		BUtils::SetLanguage();
		
		//TODO: need to set timezone by user settings. 2012.05.23
		Yii::app()->setTimeZone('Europe/Helsinki');
		
		//set the header info
		$this->header = array(
			'keywords' => BUtils::T('meta_keywords'),
			'title' => BUtils::T('meta_title'),
			'description' => BUtils::T('meta_description'),
			'image' => 'http://yamii.fi/img/logo.icon.114.png',
			'url' => Yii::app()->params['siteRoot'] . Yii::app()->request->url,
		);
		
		//cancel the duplicate load js
		Yii::app()->clientScript->scriptMap['jquery.js'] = false;
		Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
		//cancel the duplicate bootstrap
		Yii::app()->clientScript->scriptMap['bootstrap.css'] = false;
		Yii::app()->clientScript->scriptMap['bootstrap.js'] = false;
		Yii::app()->clientScript->scriptMap['bootstrap.min.css'] = false;
		Yii::app()->clientScript->scriptMap['bootstrap.min.js'] = false;
	}
	
	protected function _sendJSON($status, $message, $end = true) {
		$json_string = array(
			'code' => $status,
			'message' => $message,
		);
		echo CJSON::encode($json_string);
		if ($end) Yii::app()->end();
	}
	
	protected function _storeDownloadRef($deviceType, $url_download, $url_source) {
		$sql_download = 'INSERT INTO tbl_app_download (device, url_download, url_source, ip_address, agent)
			VALUES (:device, :url_download, :url_source, :ip_address, :agent)';
		$params = array(
			':device' => $deviceType,
			':url_download' => $url_download,
			':url_source' => $url_source,
			':ip_address' => Yii::app()->request->getUserHostAddress(),
			':agent' => Yii::app()->request->getUserAgent(),
		);
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_download);
		$command->execute($params);
		$connection->active = false;
	}
}